import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../views/Home'
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:Home
  }

]

const router = new VueRouter({
  routes
})
//挂在路由守卫（不通过登陆页面访问主页面，直接自定义跳转到登陆页面）
router.beforeEach((to,from,next) => {
  //从登陆页面跳转过去的，直接放行
  if(to.path === '/login') return next()
  //没有token验证的,跳转到登陆页面
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  //有token验证，放行
  next()
})

export default router
